package com.bm.specialvehiclefordispatcher.views;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.bm.corelibs.views.ScrollerNumberPicker;
import com.bm.corelibs.views.ScrollerNumberPicker.OnSelectListener;
import com.bm.specialvehiclefordispatcher.R;

public class DatePicker extends LinearLayout {

	public static final int DAY_COUNT = 20;

	private ScrollerNumberPicker picker1;
	private ScrollerNumberPicker picker2;
	private ScrollerNumberPicker picker3;
	private String selected;

	private List<String> outDays;
	private List<String> outHours;
	private List<String> outMins;
	
	private int nowDay = 0;

	public DatePicker(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public DatePicker(Context context) {
		super(context);
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		LayoutInflater.from(getContext()).inflate(R.layout.layout_city_picker, this);
		picker1 = (ScrollerNumberPicker) findViewById(R.id.first_picker);
		picker2 = (ScrollerNumberPicker) findViewById(R.id.second_picker);
		picker3 = (ScrollerNumberPicker) findViewById(R.id.third_picker);
		
		picker1.setData(getDays());
		picker2.setData(getNowHours());
		picker3.setData(getNowMins());
		picker1.setDefault(0);
		picker2.setDefault(0);
		picker3.setDefault(0);
		
		picker1.setOnSelectListener(new OnSelectListener() {
			@Override
			public void selecting(int id, String text) {
			}

			@Override
			public void endSelect(int id, String text) {
				nowDay = id;
				System.out.println(id);
				if(id == 0) {
					picker2.setData(getNowHours());
					picker3.setData(getNowMins());
				}
				else {
					picker2.setData(getHours());
					picker3.setData(getMins());
				}
				picker2.setDefault(0);
				picker3.setDefault(0);
			}
		});

		picker2.setOnSelectListener(new OnSelectListener() {
			@Override
			public void selecting(int id, String text) {
			}

			@Override
			public void endSelect(int id, String text) {
				if(id == 0 && nowDay == 0)
					picker3.setData(getNowMins());
				else
					picker3.setData(getMins());
				picker3.setDefault(0);
			}
		});

		picker3.setOnSelectListener(new OnSelectListener() {
			@Override
			public void selecting(int id, String text) {
			}

			@Override
			public void endSelect(int id, String text) {
			}
		});
	}

	public String getSelectedDate() {
		selected = outDays.get(picker1.getSelected()) + " " + outHours.get(picker2.getSelected()) + ":" + outMins.get(picker3.getSelected());
		return selected;
	}

	public ArrayList<String> getDays() {
		boolean flag = false;
		ArrayList<String> days = new ArrayList<String>();
		outDays = new ArrayList<String>();
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		calendar.clear();
		calendar.setTime(new Date(System.currentTimeMillis()));
		int tmpday = calendar.get(Calendar.DAY_OF_MONTH);
		for (int i = 0; i < DAY_COUNT; i++) {
			if(!flag)
				flag = tmpday > calendar.get(Calendar.DAY_OF_MONTH);
			
			String day = formatField(flag ? (calendar.get(Calendar.MONTH) + 2) : (calendar.get(Calendar.MONTH) + 1)) + "月" + formatField(calendar.get(Calendar.DAY_OF_MONTH)) + "日";
			String outDay = calendar.get(Calendar.YEAR) + "-" + formatField(flag ? (calendar.get(Calendar.MONTH) + 2) : (calendar.get(Calendar.MONTH) + 1)) + "-" + formatField(calendar.get(Calendar.DAY_OF_MONTH));
			days.add(day);
			outDays.add(outDay);
			tmpday = calendar.get(Calendar.DAY_OF_MONTH);
			calendar.roll(Calendar.DAY_OF_MONTH, true);
		}

		return days;
	}

	public ArrayList<String> getHours() {
		ArrayList<String> hours = new ArrayList<String>();
		outHours = new ArrayList<String>();
		for (int i = 0; i < 24; i++) {
			String hour = formatField(i) + "点";
			String outHour = formatField(i) + "";
			hours.add(hour);
			outHours.add(outHour);
		}
		return hours;
	}

	public ArrayList<String> getMins() {
		ArrayList<String> mins = new ArrayList<String>();
		outMins = new ArrayList<String>();
		for (int i = 0; i < 12; i++) {
			String min = formatField(i * 5) + "分";
			String outMin = formatField(i * 5) + "";
			mins.add(min);
			outMins.add(outMin);
		}
		return mins;
	}
	
	@SuppressLint("SimpleDateFormat") public ArrayList<String> getNowHours() {
		ArrayList<String> hours = new ArrayList<String>();
		outHours = new ArrayList<String>();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date(System.currentTimeMillis());
		String now = format.format(date);
		int nowHour = Integer.parseInt(now.split(" ")[1].substring(0, 2));
		int nowMin = Integer.parseInt(now.split(" ")[1].substring(3, 5));
		boolean flag = true;
		for (int i = 0; i < 24; i++) {
			if(i >= nowHour) {
				if(nowMin > 55) {
					if(flag) {
						flag = !flag;
						continue;
					}
				}
				String hour = formatField(i) + "点";
				String outHour = formatField(i) + "";
				hours.add(hour);
				outHours.add(outHour);
			}
		}
		return hours;
	}
	
	@SuppressLint("SimpleDateFormat") public ArrayList<String> getNowMins() {
		ArrayList<String> mins = new ArrayList<String>();
		outMins = new ArrayList<String>();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date(System.currentTimeMillis());
		String now = format.format(date);
		int nowMin = Integer.parseInt(now.split(" ")[1].substring(3, 5));
		for (int i = 0; i < 12; i++) {
			if(i * 5 >= nowMin || nowMin > 55) {
				String min = formatField(i * 5) + "分";
				String outMin = formatField(i * 5) + "";
				mins.add(min);
				outMins.add(outMin);
			}
		}
		return mins;
	}
	
	public String formatField(int i) {
		return i < 10 ? ("0" + i) : ("" + i);
	}

}
